﻿using ShiQuan.IotDevice.Business;
using ShiQuan.IotDevice.Entity;
using ShiQuan.WebBase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace ShiQuan.IotDevice.Web.Api
{
    /// <summary>
    /// 检测结果上传
    /// </summary>
    public class DetectionResultController : BaseApiController
    {
        /// <summary>
        /// 获取服务器时间
        /// </summary>
        /// <returns></returns>
        public AjaxResult GetTime()
        {
            return this.Success("读取成功", DateTime.Now.ToString());
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public AjaxResult SaveData()
        {
            try
            {
                HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
                HttpRequestBase requestBase = context.Request;//定义传统request对象  
                var source = Newtonsoft.Json.JsonConvert.DeserializeObject<DetectionResultEntity>(requestBase["entity"]);
                DetectionResultBLL service = new DetectionResultBLL();
                service.SaveForm(source.Id,source);

                return this.Success("上传成功");
            }
            catch (Exception ex)
            {
                return this.Error(ex.Message);
            }
        }
        /// <summary>
        /// 保存数据-爱德克斯
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public AjaxResult SaveAdvics()
        {
            try
            {
                var stream = HttpContext.Current.Request.InputStream;
                if (stream.Length <= 0)
                    return this.Warning("上传内容不能为空！" );
                var data = string.Empty;
                using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8))
                {
                    data = reader.ReadToEnd();
                }
                if (data.IndexOf('=') == -1)
                {
                    return this.Warning("数据格式不符合要求！");
                }
                var modeJson = HttpUtility.UrlDecode(data.Split('=')[1]);

                data += "\n" + modeJson;
                var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".log";
                var filePath = HttpContext.Current.Server.MapPath("/Upload/DetectionResult");
                if (System.IO.Directory.Exists(filePath) == false)
                    System.IO.Directory.CreateDirectory(filePath);

                System.IO.File.WriteAllText(System.IO.Path.Combine(filePath,fileName), data);

                var model = Newtonsoft.Json.JsonConvert.DeserializeObject<AdvicsModel>(modeJson);
                DetectionResultBLL service = new DetectionResultBLL();
                service.SaveAdvics(model);

                return this.Success("上传成功");
            }
            catch (Exception ex)
            {
                return this.Error(ex.Message);
            }
        }
    }

    
}
